#include "FileProcessor.h"
#include "tinyxml2.h"

#include <iostream>
#include <sstream>
// #include <fstream>
#include <string>
#include <vector>
#include <regex>
#include "PageLib.h"

using std::string;
using std::vector;
using std::cout;
using std::endl;

using namespace tinyxml2;

int FileProcessor::process(const string& fileName) {
    // cout << "第一次进入process" << endl;
    XMLDocument doc;
    doc.LoadFile(fileName.c_str());

    int index_now = _pageLib.getPages().size();
    if (doc.LoadFile(fileName.c_str()) == XML_SUCCESS) {
        XMLElement* channelElement = doc.FirstChildElement("rss")->FirstChildElement("channel");
        XMLElement* itemElement = channelElement->FirstChildElement("item");

        while (itemElement != nullptr) {
            WebPage tmpWP;
            XMLElement* titleElement = itemElement->FirstChildElement("title");
            XMLElement* linkElement = itemElement->FirstChildElement("link");

            XMLElement* descriptionElement = itemElement->FirstChildElement("description");
            

            string linkTxt, titleTxt, descriptionTxt;
            if (linkElement) {
                linkTxt = linkElement->GetText();
            }

            if (titleElement) {
                titleTxt = titleElement->GetText();
            }

            if (descriptionElement) {
                descriptionTxt = descriptionElement->GetText();
                std::regex tagRegex("<[^>]*>");
                descriptionTxt = std::regex_replace(descriptionTxt, tagRegex, "");
            }

            tmpWP._docId = index_now;
            tmpWP._docTitle = titleTxt;
            tmpWP._docUrl = linkTxt;
            tmpWP._docContent = descriptionTxt;

            std::stringstream ss;
            ss << "<doc><docid>" << index_now++ << "</docid><url>" << linkTxt 
                << "</url><title>" << titleTxt << "</title><content>" 
                << descriptionTxt << "</content></doc>";

            string fmtTxt = ss.str();
            _pageLib.getPages().push_back(fmtTxt);
            _pageLib.getWebPage().push_back(tmpWP);

            itemElement = itemElement->NextSiblingElement("item");
        }

    } else {
        cout << "Failed to load xml file." << endl;

        return -1;
    }

    // cout << fileName << "处理完毕" << endl;
    // cout << _pageLib.getFiles()[_pageLib.getFiles().size() - 1] << endl;
    return 0;
}
